#include <allegro.h>
#include <math.h>
//#include <windows.h>
#include <iostream>

using namespace std;


static int WIDTH = 640;
static int HEIGHT = 700;

//start timer junk
volatile long speed_counter = 0;

void increment_speed_counter()
{
     speed_counter++;
}
END_OF_FUNCTION(increment_speed_counter);
//timer junk

void init();
void deinit();

int main() {
	init();
	
	//starting randoms variables
		LOCK_VARIABLE(speed_counter);
		LOCK_FUNCTION(increment_speed_counter);
		install_int_ex(increment_speed_counter, BPS_TO_TIMER(750));
		
		int pacx = 500;
		int pacy = 0;
		int ghostx = 0;
		int ghosty = 0;
		int frame_counter;
		
		BITMAP *pac1 = load_bitmap("pac1.bmp", NULL);//loads bitmap
		BITMAP *pac2 = load_bitmap("pac2.bmp", NULL);
		BITMAP *pac3 = load_bitmap("pac4.bmp", NULL);
		BITMAP *ghost1 = load_bitmap("ghost1.bmp", NULL);
		BITMAP *buffer = create_bitmap(WIDTH,HEIGHT);
		
		SAMPLE *test = load_sample("test2.wav");//**only 8bit or 16bit wav files work
		SAMPLE *test1 = load_sample("test1.wav");
		
		//create boundary boxes
		int pacBBL = pacx;
		int pacBBT = pacy;
		int pacBBR = (pacBBL + pac3->w);
		int pacBBB = (pacBBT + pac3->h);
		
		int ghostBBL = ghostx;
		int ghostBBT = ghosty;
		int ghostBBR = (ghostBBL + ghost1->w);
		int ghostBBB = (ghostBBT + ghost1->h);
		
  //endvariabls
  
  //*****If you want a song add here before while or else it loopps too fast
	    
	while (!key[KEY_ESC]) {
		/* put your code here */
		
		 
		

	    
	    while(speed_counter > 0)
	    {//start time count
		
		//start pacman controls
		if(key[KEY_RIGHT])
		{
             pacx++;
             if (pacx > 640)
             pacx = -75;
             
        }
        if(key[KEY_DOWN])
        {
             pacy++;
             if (pacy > 700)
             pacy = -75;

        }
        if(key[KEY_LEFT])
        {
             pacx--;
             if (pacx < -75)
             pacx = 640;
        }
        if(key[KEY_UP])
        {
             pacy--;
             if (pacy < -75)
             pacy = 700;

        }
        //end pacman controls
        
        //start ghost controls
        
        if(key[KEY_A])
        ghostx--;
        if (ghostx < -75){
             ghostx = 640;}
        
        if(key[KEY_D])
        ghostx++;
        if (ghostx > 640){
             ghostx = -75;}
        
        if(key[KEY_S])
        ghosty++;
        if (ghosty > 700){
             ghosty = -75;}
        
        if(key[KEY_W])
        ghosty--;
        if (ghosty < -75){
             ghosty = 700;}
        //end ghost controls
        
        //start music board
        if(key[KEY_1])
        {//stop_sample(test1); **might be needed?
        play_sample(test, 255, 128, 1000, 1);}
        if(key[KEY_2])
        {//stop_sample(test);  
        play_sample(test1, 255, 128, 1000, 1);}
        
        
        
        speed_counter--;
        frame_counter++;
        if(frame_counter > 240)
        {
                         frame_counter = 0;
        }
        }//end timer
        
        //start animation counter
        if(frame_counter < 60)
        {
             draw_sprite(buffer, pac3, pacx, pacy);
        }
        else if(frame_counter >= 60 && frame_counter <120)
        {
             draw_sprite(buffer, pac2, pacx, pacy);
        }
        else if(frame_counter >= 120 && frame_counter <180)
        {
             draw_sprite(buffer, pac1, pacx, pacy);
        }
        else
        {
            draw_sprite(buffer, pac2, pacx, pacy);
        }
        
        draw_sprite(buffer, ghost1, ghostx, ghosty);//draw ghost
        
        //collision
        if((abs(pacx-ghostx)+abs(pacy-ghosty)) < 100){
        pacx = 0; 
        pacy = 0;}


	blit(buffer,screen,0,0,0,0,WIDTH,HEIGHT);
	clear_bitmap(buffer);
	

	
	//readkey();

	}
	destroy_sample(test);
	destroy_sample(test1);
	remove_sound();
	destroy_bitmap(pac1);
    destroy_bitmap(pac2);
    destroy_bitmap(pac3);
    destroy_bitmap(buffer);

	deinit();
	return 0;
}
END_OF_MAIN()

void init() {
	int depth, res;
	allegro_init();
	depth = desktop_color_depth();
	if (depth == 0) depth = 32;
	set_color_depth(depth);
	res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, WIDTH, HEIGHT, 0, 0);
	if (res != 0) {
		allegro_message(allegro_error);
		exit(-1);
	}

	install_timer();
	install_keyboard();
	install_mouse();
    install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, 0); 
	
	
	/* add other initializations here */
}

void deinit() {
	clear_keybuf();
	/* add other deinitializations here */
}
